package com.haoze.nurseapp.controller;

import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.common.utils.DateUtil;
import com.haoze.nurseapp.dto.GeneralCheckLogDTO;
import com.haoze.nurseapp.dto.NurseOrdersReviewDTO;
import com.haoze.nurseapp.dto.OrderPageDto;
import com.haoze.nurseapp.dto.OrdersReviewDTO;
import com.haoze.nurseapp.enumeration.OrderQueryStatusTypeEnum;
import com.haoze.nurseapp.service.OrderService;
import com.haoze.nurseapp.service.PatientCirculationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/nurseapp/order")
@Api(value = "医嘱信息目录")
@Validated
public class OrderController {

    @Resource
    OrderService orderService;

    @Autowired
    PatientCirculationService patientCirculationService;

//	@GetMapping("/orderInfo")
//	@ApiOperation(value = "医嘱信息")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "inpatNo", value = "住院号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
//			@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query", example = "2020-05-05", required = true),
//			@ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
//			@ApiImplicitParam(name = "type", value = "医嘱类型", dataType = "String", paramType = "query", example = "long长期医嘱，tmp临时医嘱", required = false),
//			@ApiImplicitParam(name = "status", value = "医嘱状态", dataType = "String", paramType = "query", example = "new新开医嘱，unload未执行，loading执行中，loaded已执行", required = false),
//			@ApiImplicitParam(name = "orderTypeId", value = "医嘱分类id", dataType = "String", paramType = "query", example = "", required = false)
//	})
//	public Result orderInfo(@RequestParam(required = false) String orderTypeId, @RequestParam String inpatNo, @RequestParam String startDate, @RequestParam String endDate, @RequestParam(required = false) String type, @RequestParam(required = false) String status, @RequestParam Integer page, @RequestParam Integer pageSize) {
//		return orderService.orderInfo(orderTypeId, inpatNo, startDate, endDate, null, type, status, page, pageSize);
//	}


	@GetMapping("/orderInfo")
	@ApiOperation(value = "医嘱信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "inpatNo", value = "住院号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
			@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query", example = "2020-05-05", required = true),
			@ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
			@ApiImplicitParam(name = "type", value = "医嘱类型", dataType = "String", paramType = "query", example = "long长期医嘱，tmp临时医嘱", required = false),
			@ApiImplicitParam(name = "status", value = "医嘱状态", dataType = "String", paramType = "query", example = "new新开医嘱，unload未执行，loading执行中，loaded已执行", required = false),
			@ApiImplicitParam(name = "orderTypeId", value = "医嘱分类id", dataType = "String", paramType = "query", example = "", required = false)
	})
	public Result orderInfo(@RequestParam(required = false) String orderTypeId,
							@RequestParam String inpatNo,
							@RequestParam String startDate,
							@RequestParam String endDate,
							@RequestParam(required = false) String type,
							@RequestParam(required = false) String search,
							@RequestParam(required = false) String status,
							@RequestParam Integer page,
							@RequestParam Integer pageSize) {
		if (org.apache.commons.lang3.StringUtils.isNotBlank(startDate)) {
			startDate += " 00:00:00";
		}
		if (org.apache.commons.lang3.StringUtils.isNotBlank(endDate)) {
			endDate += " 23:59:59";
		}
		OrderPageDto orderPageDto = new OrderPageDto();
		orderPageDto.setPageNum(page);
		orderPageDto.setPageSize(pageSize);
		orderPageDto.setStatus(status);
		orderPageDto.setType(type);
		orderPageDto.setStartDate(startDate);
		orderPageDto.setEndDate(endDate);
		orderPageDto.setOrderTypeId(orderTypeId);
		orderPageDto.setInpatNo(inpatNo);
		orderPageDto.setSearch(search);
        Date now = new Date();
        orderPageDto.setDateTime(DateUtil.dateToString(now,"yyyy-MM-dd"));

		return ResultGenerator.genOkResult(orderService.queryPagesList(orderPageDto));
	}


    @GetMapping("/orderType")
    @ApiOperation(value = "医嘱分类")
    public Result orderType() {
        return orderService.orderType();
    }

    @GetMapping("/orderInfoApp")
    @ApiOperation(value = "医嘱信息列表App")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "times", value = "时间", example = "2020-05-05", required = true),
            @ApiImplicitParam(name = "inpatNo", value = "住院号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "type", value = "医嘱类型", dataType = "String", paramType = "query", example = "long长期医嘱，tmp临时医嘱", required = false),
            @ApiImplicitParam(name = "orderStatusTypeValue", value = "医嘱状态(1-全部 2-整理 Arrange3-新开 Stoped 4-已停)")
    })
    public Result orderInfoApp(
            @RequestParam String inpatNo,
            @RequestParam String times,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Integer orderStatusTypeValue,
            @RequestParam Integer page,
            @RequestParam Integer pageSize) {
        if (StringUtils.isEmpty(times)) {
            throw new RuntimeException("请选择时间");
        }
        OrderPageDto orderPageDto = new OrderPageDto();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(times)) {
            times += " 23:59:59";
            orderPageDto.setEndDate(times);
        }
        orderPageDto.setPageNum(page);
        orderPageDto.setPageSize(pageSize);
        orderPageDto.setType(type);
        orderPageDto.setInpatNo(inpatNo);
        if (orderStatusTypeValue != null && orderStatusTypeValue.intValue() > 0) {
            orderPageDto.setOrderQueryStatusValue(orderStatusTypeValue);
            orderPageDto.setOrderQueryStatusType(OrderQueryStatusTypeEnum.getById(orderStatusTypeValue));
        }

        return ResultGenerator.genOkResult(orderService.queryPagesListApp(orderPageDto));
    }


    @GetMapping("/orderDetail")
    @ApiOperation(value = "医嘱明细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatorderdataId", value = "医嘱编号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query", example = "2020-05-05", required = true),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
    })
    public Result orderDetail(@RequestParam String inpatorderdataId, @RequestParam String startDate, @RequestParam String endDate, @RequestParam Integer page, @RequestParam Integer pageSize) {
        return orderService.orderDetail(inpatorderdataId, startDate, endDate, page, pageSize);
    }

    @GetMapping("/orderInfoByOrderDataId")
    @ApiOperation(value = "医嘱信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "医嘱编号", value = "", dataType = "String", paramType = "query", example = "12345678_1", required = true),
    })
    public Result orderInfoByOrderDataId(@RequestParam String inpatorderdataId) {
        return orderService.orderInfoByOrderDataId(inpatorderdataId);
    }

    /**
     * 医嘱未复核列表
     * @param inpatList
     * @param startDatetime
     * @param endDateTime
     * @return
     */
    @GetMapping("/reviewList")
    @ApiOperation(value = "提取复核列表")
    public Result reviewList(@RequestParam String inpatList, @RequestParam(required = false) String startDatetime,
                             @RequestParam(required = false) String endDateTime,
                             @RequestParam(required = false) String type,
                             @RequestParam(required = false) String bedNo) {
        return orderService.reviewList(inpatList, startDatetime, endDateTime,type,bedNo);
    }

    /**
     * 医嘱复核功能
     * @param nurseOrdersReviewDTO
     * @return
     */
    @PostMapping("/patientOrdersReview")
    @ApiOperation(value = "医嘱复核功能")
    //public Result patientOrdersReview(@RequestBody List<OrdersReviewDTO> inpatorderdataIds) {
    public Result patientOrdersReview(@RequestBody NurseOrdersReviewDTO nurseOrdersReviewDTO) {
        return orderService.patientOrdersReview(nurseOrdersReviewDTO);
    }

    /**
     * 医生站医嘱复核功能
     * @param ordersReviewDTOList
     * @return
     */
    @PostMapping("/patientOrdersReviewDoctor")
    @ApiOperation(value = "医嘱复核功能")
    public Result patientOrdersReviewDoctor(@RequestBody List<OrdersReviewDTO> ordersReviewDTOList) {
        return orderService.patientOrdersReviewDoctor(ordersReviewDTOList);
    }

    /**
     * 医嘱未复核/已复核列表病人列表
     * @return
     */
    @GetMapping("/queryPatientBedNoList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "0 在院 1出院", dataType = "String", paramType = "query",required = false)
    })
    public Result queryPatientBedNoList(@RequestParam(required = false) String wardDeptId,
                                        @RequestParam String inpatList, @RequestParam(required = false) String startDatetime,
                                        @RequestParam(required = false) String endDateTime,
                                        @RequestParam(required = false) String type, @RequestParam(required = false) String status) {
        return orderService.queryPatientBedNoList(wardDeptId,inpatList, startDatetime, endDateTime,type,status);
    }

    /**
     * 医嘱未复核列表病人列表
     * @return
     */
    @GetMapping("/notReviewPatientList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "0 在院 1出院", dataType = "String", paramType = "query",required = false)
    })
    public Result notReviewPatientList(@RequestParam String wardDeptId,@RequestParam(required = false) String status) {
        return orderService.notReviewPatientList(wardDeptId,status);
    }


    /**
     * 医嘱核对列表
     * @param inpatList
     * @param startDatetime
     * @param endDateTime
     * @return
     */
    @GetMapping("/ordersCheckList")
    @ApiOperation(value = "医嘱核对列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatList", value = "医嘱编号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "startDatetime", value = "开始时间", dataType = "String", paramType = "query", example = "2020-05-05", required = true),
            @ApiImplicitParam(name = "endDateTime", value = "结束时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
            @ApiImplicitParam(name = "orderType", value = "0 全部、1长期医嘱、2临时医嘱", dataType = "String", paramType = "query", example = "0", required = true),
            @ApiImplicitParam(name = "status", value = "0未核对 1已核对", dataType = "String", paramType = "query", example = "0", required = true),
    })
    public Result ordersCheckList(@RequestParam String inpatList,
                                  @RequestParam(required = false) String startDatetime,
                                  @RequestParam(required = false) String endDateTime,
                                  @RequestParam String orderType,
                                  @RequestParam String status) {
        return orderService.ordersCheckList(inpatList, startDatetime, endDateTime,orderType,status);
    }

    /**
     * 医嘱核对保存
     * @param inpatorderdataIds
     * @return
     */
    @PostMapping("/saveOrdersCheck")
    @ApiOperation(value = "医嘱核对保存功能")
    public Result saveOrdersCheck(@RequestBody List<OrdersReviewDTO> inpatorderdataIds) {
        return orderService.saveOrdersCheck(inpatorderdataIds);
    }


    /**
     * 医嘱总对列表
     * @param inpatList
     * @return
     */
    @GetMapping("/ordersGeneralCheckList")
    @ApiOperation(value = "医嘱总对列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatList", value = "医嘱编号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "orderType", value = "0 整理医嘱、1长期医嘱、2临时医嘱", dataType = "String", paramType = "query", example = "0", required = true),
            @ApiImplicitParam(name = "status", value = "0总对医嘱 1查询医嘱", dataType = "String", paramType = "query", example = "0", required = true)
    })
    public Result ordersGeneralCheckList(@RequestParam String inpatList,
                                        @RequestParam String orderType,
                                        @RequestParam String status) {
        return orderService.ordersGeneralCheckList(inpatList,orderType,status);
    }

    /**
     * 医嘱总对列表各个类型的count值
     * @param inpatList
     * @return
     */
    @GetMapping("/ordersGeneralCheckTypeCount")
    @ApiOperation(value = "医嘱总对列表各个类型的count值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatList", value = "医嘱编号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "orderType", value = "0 整理医嘱、1长期医嘱、2临时医嘱", dataType = "String", paramType = "query", example = "0", required = true),
            @ApiImplicitParam(name = "status", value = "0总对医嘱 1查询医嘱", dataType = "String", paramType = "query", example = "0", required = true)
    })
    public Result ordersGeneralCheckTypeCount(@RequestParam String inpatList,
                                         @RequestParam String status) {
        return orderService.ordersGeneralCheckTypeCount(inpatList,status);
    }

    /**
     *  获取病区对应的所有护士
     */
    @GetMapping("/queryNurseListByWardDeptId")
    @ApiOperation(value = "获取病区所有护士")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardDeptId", value = "病区Id", dataType = "String", paramType = "wardDeptId")
    })
    public Result queryNurseListByWardDeptId(@RequestParam String wardDeptId){
        return patientCirculationService.queryNurseListByWardDeptId(wardDeptId);
    }
    /**
     * 总对保存
     * @param generalCheckLogDTO
     * @return
     */
    @PostMapping("/saveGeneralCheck")
    @ApiOperation(value = "医嘱核对保存功能")
    public Result saveGeneralCheck(@RequestBody GeneralCheckLogDTO generalCheckLogDTO) {
        return orderService.saveGeneralCheck(generalCheckLogDTO);
    }


    @GetMapping("/useMedicationList")
    @ApiOperation(value = "患者用药列表")
    public Result useMedicationList(@RequestParam String inpatNum,
                                    @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "20") Integer pageSize,
                                    @RequestParam(required = false) String keyWords,
                                    @RequestParam(required = false) String type) {
        return orderService.useMedicationList(inpatNum, page, pageSize,keyWords,type);
    }


    @GetMapping("/inpRecByOrderId")
    @ApiOperation(value = "根据医嘱查询药品明细")
    public Result inpRecByOrderId(@RequestParam(required = false) String orderId,@RequestParam(required = false) String prescNum) {
        return orderService.inpRecByOrderId(orderId,prescNum);
    }


    @GetMapping("/queryStaticList")
    @ApiOperation(value = "静配未通过医嘱审核查询")
    public Result queryStaticList(@RequestParam(required = false) String startDateTime,
                                  @RequestParam(required = false) String endDateTime,
                                  @RequestParam(required = false) String wardId) {
        return orderService.queryStaticList(startDateTime, endDateTime,wardId);
    }

    @GetMapping("/queryOrderBookKeepingTotalCost")
    @ApiOperation(value = "医嘱记账总费用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", required = true),
            @ApiImplicitParam(name = "type", value = "0 全部医嘱，1 未收费医嘱，2 已收费医嘱", dataType = "String", required = true)
    })
    public Result queryOrderBookKeepingTotalCost(@RequestParam(required = true) String inpatNum) {
        return orderService.queryOrderBookKeepingTotalCost(inpatNum);
    }


    @GetMapping("/queryOrdersBookKeeping")
    @ApiOperation(value = "医嘱记账查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", required = true),
            @ApiImplicitParam(name = "type", value = "0 全部医嘱，1 未收费医嘱，2 已收费医嘱", dataType = "String", required = true)
    })
    public Result queryOrdersBookKeeping(@RequestParam(required = true) String inpatNum,
                                         @RequestParam(required = false) String type,
                                         @RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "20") Integer pageSize) {
        return orderService.queryOrdersBookKeeping(inpatNum, type,page,pageSize);
    }

    /**
     * 补划价
     * @param inpatorderdataIds
     * @return
     */
    @PostMapping("/ordersSupplement")
    @ApiOperation(value = "补划价")
    public Result ordersSupplement(@RequestBody List<OrdersReviewDTO> inpatorderdataIds) {
        return orderService.ordersSupplement(inpatorderdataIds);
    }

    /**
     * 获取已记账的费用信息
     * @param inpatorderdataIds
     * @return
     */
    @PostMapping("/queryCostByOrderId")
    @ApiOperation(value = "获取已记账的费用信息")
    public Result queryCostByOrderId(@RequestBody OrdersReviewDTO inpatorderdataIds) {
        return orderService.queryCostByOrderId(inpatorderdataIds);
    }


}
